// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <nebula/util/formatting.h>
#include <nebula/version.h>
#include <turbo/numeric/float16.h>
#include <turbo/log/logging.h>

namespace nebula {
    using stevin::conversion::DoubleToStringConverter;

    static constexpr int kMinBufferSize = DoubleToStringConverter::kBase10MaximalLength + 1;

    namespace internal {
        namespace detail {
            const char digit_pairs[] =
                    "0001020304050607080910111213141516171819"
                    "2021222324252627282930313233343536373839"
                    "4041424344454647484950515253545556575859"
                    "6061626364656667686970717273747576777879"
                    "8081828384858687888990919293949596979899";
        } // namespace detail

        struct FloatToStringFormatter::Impl {
            Impl()
                : converter_(DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN, "inf", "nan",
                             'e', -6, 10, 6, 0) {
            }

            Impl(int flags, const char *inf_symbol, const char *nan_symbol, char exp_character,
                 int decimal_in_shortest_low, int decimal_in_shortest_high,
                 int max_leading_padding_zeroes_in_precision_mode,
                 int max_trailing_padding_zeroes_in_precision_mode)
                : converter_(flags, inf_symbol, nan_symbol, exp_character, decimal_in_shortest_low,
                             decimal_in_shortest_high, max_leading_padding_zeroes_in_precision_mode,
                             max_trailing_padding_zeroes_in_precision_mode) {
            }

            DoubleToStringConverter converter_;
        };

        FloatToStringFormatter::FloatToStringFormatter() : impl_(new Impl()) {
        }

        FloatToStringFormatter::FloatToStringFormatter(
            int flags, const char *inf_symbol, const char *nan_symbol, char exp_character,
            int decimal_in_shortest_low, int decimal_in_shortest_high,
            int max_leading_padding_zeroes_in_precision_mode,
            int max_trailing_padding_zeroes_in_precision_mode)
            : impl_(std::make_unique<Impl>(flags, inf_symbol, nan_symbol, exp_character,
                                           decimal_in_shortest_low, decimal_in_shortest_high,
                                           max_leading_padding_zeroes_in_precision_mode,
                                           max_trailing_padding_zeroes_in_precision_mode)) {
        }

        FloatToStringFormatter::~FloatToStringFormatter() {
        }

        int FloatToStringFormatter::FormatFloat(float v, char *out_buffer, int out_size) {
            DKCHECK_GE(out_size, kMinBufferSize);
            // StringBuilder checks bounds in debug mode for us
            stevin::conversion::StringBuilder builder(out_buffer, out_size);
            bool result = impl_->converter_.ToShortestSingle(v, &builder);
            DKCHECK(result);
            TURBO_UNUSED(result);
            return builder.position();
        }

        int FloatToStringFormatter::FormatFloat(double v, char *out_buffer, int out_size) {
            DKCHECK_GE(out_size, kMinBufferSize);
            stevin::conversion::StringBuilder builder(out_buffer, out_size);
            bool result = impl_->converter_.ToShortest(v, &builder);
            DKCHECK(result);
            TURBO_UNUSED(result);
            return builder.position();
        }

        int FloatToStringFormatter::FormatFloat(uint16_t v, char *out_buffer, int out_size) {
            DKCHECK_GE(out_size, kMinBufferSize);
            stevin::conversion::StringBuilder builder(out_buffer, out_size);
            bool result = impl_->converter_.ToShortest(turbo::Float16::from_bits(v).to_float(), &builder);
            DKCHECK(result);
            TURBO_UNUSED(result);
            return builder.position();
        }
    } // namespace internal
} // namespace nebula
